<template>
  <div class="app-container">
    <el-row style="margin-bottom: 10px">
      <el-col :span="10">
        <el-radio-group v-model="queryParams.status" @change="getList">
          <el-radio-button :label="''">全部</el-radio-button>
          <el-radio-button v-for="item in statusList" :label="item.value" >{{ item.label }}</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col :span="7">
        <el-date-picker
          v-model="dateList"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="getList"
        >
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-button
          type="primary"
          icon="el-icon-arrow-down"
          size="mini"
          style="margin-left: 10px"
          @click="handleShow"
        > </el-button>
      </el-col>
      <el-col :span="2" style="text-align: right">
        <el-button
          icon="el-icon-delete"
          size="mini"
          @click="handleRecycle"
        >回收站</el-button>
      </el-col>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分销商ID" prop="resellerId">
        <el-input
          v-model="queryParams.resellerId"
          placeholder="请输入分销商ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="用户" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="语言" prop="langId">
        <el-select v-model="queryParams.langId" placeholder="请选择">
          <el-option
            v-for="item in langList"
            :key="item.id"
            :label="item.langCn"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="平台" prop="platform">
        <el-select v-model="queryParams.platform" placeholder="请选择">
          <el-option
            v-for="item in platformList"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付货币" prop="currency">
        <el-select v-model="queryParams.currency" placeholder="请选择">
          <el-option
            v-for="item in langList"
            :key="item.id"
            :label="item.currency"
            :value="item.currency">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择">
          <el-option
            v-for="item in payTypeList"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="有效期" prop="times">-->
<!--        <el-input-->
<!--          v-model="queryParams.times"-->
<!--          placeholder="请输入有效期"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付金额" prop="totalFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalFee"-->
<!--          placeholder="请输入支付金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="实际支付金额" prop="payFee">-->
<!--        <el-input-->
<!--          v-model="queryParams.payFee"-->
<!--          placeholder="请输入实际支付金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="交易单号" prop="transactionId">-->
<!--        <el-input-->
<!--          v-model="queryParams.transactionId"-->
<!--          placeholder="请输入交易单号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="支付时间" prop="paytime">-->
<!--        <el-input-->
<!--          v-model="queryParams.paytime"-->
<!--          placeholder="请输入支付时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updatetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.updatetime"-->
<!--          placeholder="请输入更新时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="删除时间" prop="deletetime">-->
<!--        <el-input-->
<!--          v-model="queryParams.deletetime"-->
<!--          placeholder="请输入删除时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-descriptions direction="vertical" :column="5" border style="font-weight: bold;font-size: 16px;margin-bottom: 10px">
      <el-descriptions-item label="总订单数">{{ totalMap.totalCount }}</el-descriptions-item>
      <el-descriptions-item label="支付单数">{{ totalMap.paymentOrder }}</el-descriptions-item>
      <el-descriptions-item label="未支付单数">{{ totalMap.unpaidOrder }}</el-descriptions-item>
      <el-descriptions-item label="订单总金额">{{ totalMap.totalOrderAmount }}</el-descriptions-item>
      <el-descriptions-item label="支付总金额">{{ totalMap.totalPaymentAmount }}</el-descriptions-item>
    </el-descriptions>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="${comment}" align="center" prop="siteId" />-->
      <el-table-column label="分销商ID" align="center" prop="resellerId" />
      <el-table-column label="订单号" width="200px" align="center" prop="orderSn" />
      <el-table-column label="用户" align="center" prop="userId" />
      <el-table-column label="语言" align="center" prop="langName" />
<!--      <el-table-column label="有效期" align="center" prop="times" />-->
      <el-table-column label="订单状态" align="center" prop="status" >
        <template slot-scope="scope">
          {{ statusText(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="支付金额" align="center" prop="totalFee" />
      <el-table-column label="实际支付金额" align="center" prop="payFee" />
      <el-table-column label="货币" align="center" prop="currency" />
<!--      <el-table-column label="交易单号" align="center" prop="transactionId" />-->
<!--      <el-table-column label="交易原始数据" align="center" prop="paymentJson" />-->
<!--      <el-table-column label="订单备注" align="center" prop="remark" />-->
      <el-table-column label="支付方式" align="center" prop="payType" >
        <template slot-scope="scope">
          {{ payTypeText(scope.row.payType) }}
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="paytime" />
<!--      <el-table-column label="附加字段" align="center" prop="ext" />-->
      <el-table-column label="平台" align="center" prop="platform" >
        <template slot-scope="scope">
          {{ platformText(scope.row.platform) }}
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" align="center" prop="createtime" />-->
<!--      <el-table-column label="更新时间" align="center" prop="updatetime" />-->
<!--      <el-table-column label="删除时间" align="center" prop="deletetime" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分销订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分销商ID" prop="resellerId">
          <el-input v-model="form.resellerId" placeholder="请输入分销商ID" />
        </el-form-item>
        <el-form-item label="订单号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="用户" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="语言" prop="langId">
          <el-input v-model="form.langId" placeholder="请输入语言" />
        </el-form-item>
        <el-form-item label="有效期" prop="times">
          <el-input v-model="form.times" placeholder="请输入有效期" />
        </el-form-item>
        <el-form-item label="支付金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="实际支付金额" prop="payFee">
          <el-input v-model="form.payFee" placeholder="请输入实际支付金额" />
        </el-form-item>
        <el-form-item label="货币" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入货币" />
        </el-form-item>
        <el-form-item label="交易单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入交易单号" />
        </el-form-item>
        <el-form-item label="交易原始数据" prop="paymentJson">
          <el-input v-model="form.paymentJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="订单备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="支付时间" prop="paytime">
          <el-input v-model="form.paytime" placeholder="请输入支付时间" />
        </el-form-item>
        <el-form-item label="附加字段" prop="ext">
          <el-input v-model="form.ext" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除时间" prop="deletetime">
          <el-input v-model="form.deletetime" placeholder="请输入删除时间" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 回收站管理 -->
    <el-dialog title="回收站" :visible.sync="openDel" append-to-body>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-refresh-right"
            size="mini"
            :disabled="single"
            @click="handleReduction"
          >还原</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="single"
            @click="handleDestruction"
          >销毁</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-refresh-right"
            size="mini"
            @click="batchReduction"
          >还原全部</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="batchDestruction"
          >清空回收站</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="delLoading" :data="delList" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="删除时间" align="center" prop="deletetime" />
        <el-table-column fixed="right" width="150px" label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="clickReduction(scope.row)"
            >还原</el-button>
            <el-button
              size="mini"
              type="text"
              @click="clickDestruction(scope.row)"
            >销毁</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="delTotal>0"
        :total="delTotal"
        :page.sync="delParams.pageNum"
        :limit.sync="delParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, selectCount } from "@/api/reseller/order";
import {listLang} from "@/api/video/lang";

export default {
  name: "Order",
  data() {
    return {
      totalMap: {
        totalCount: '0',
        paymentOrder: '0',
        unpaidOrder: '0',
        totalOrderAmount: 0,
        totalPaymentAmount: 0,
      },
      langList: [],
      // 支付状态下拉框
      statusList:[ // 待循环的选项数组
        { label: '交易关闭', value: '-2' },
        { label: '已取消', value: '-1' },
        { label: '未支付', value: '0' },
        { label: '已支付', value: '1' },
        { label: '已完成', value: '2' }
        // 可以根据需要添加更多选项
      ],
      // 支付平台下拉框
      platformList: [
        { label: 'H5', value: 'H5' },
        // { label: '微信公众号', value: 'wxOfficialAccount' },
        // { label: '微信小程序', value: 'wxMiniProgram' },
        // { label: 'Web', value: 'Web' },
        { label: 'APP', value: 'App' }
      ],
      // 支付方式下拉框
      payTypeList:[ // 待循环的选项数组
        // { label: '微信支付', value: 'wechat' },
        // { label: '支付宝', value: 'alipay' },
        // { label: '钱包支付', value: 'wallet' },
        // { label: '积分支付', value: 'score' },
        { label: '卡密兑换', value: 'cryptocard' },
        { label: '管理员设置', value: 'system' },
        { label: 'PayPal', value: 'paypal' },
        { label: 'Stripe', value: 'stripe' }
      ],
      dateList: [],
      openDel: false,
      delList: [],
      delLoading: false,
      delTotal: 0,
      delParams: {
        pageNum: 1,
        pageSize: 10,
        deletetime: 1
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 分销订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        resellerId: null,
        orderSn: null,
        userId: null,
        langId: '',
        times: null,
        status: '',
        totalFee: null,
        payFee: null,
        currency: null,
        transactionId: null,
        paymentJson: null,
        payType: null,
        paytime: null,
        beginPaytime: null,
        endPaytime: null,
        ext: null,
        platform: null,
        createtime: null,
        updatetime: null,
        deletetime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        resellerId: [
          { required: true, message: "分销商ID不能为空", trigger: "blur" }
        ],
        orderSn: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成不能为空", trigger: "change" }
        ],
        totalFee: [
          { required: true, message: "支付金额不能为空", trigger: "blur" }
        ],
        payFee: [
          { required: true, message: "实际支付金额不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getLangList();
  },
  methods: {
    getTotalMap() {
      this.loading = true;
      selectCount(this.queryParams).then(response => {
        this.totalMap = response.data;
        this.loading = false;
      });
    },
    getLangList() {
      this.loading = true;
      listLang().then(response => {
        this.langList = response.rows;
        this.loading = false;
      });
    },
    /** 隐藏/展示查询列表 */
    handleShow() {
      this.showSearch = !this.showSearch;
    },
    /** 回收站dialog */
    handleRecycle() {
      this.openDel = true;
      this.getDelList();
    },
    /** 查询回收站VIP订单列表 */
    getDelList() {
      this.delLoading = true;
      listOrder(this.delParams).then(response => {
        this.delList = response.rows;
        this.delTotal = response.total;
        this.delLoading = false;
      });
    },
    /** 多选还原数据 */
    handleReduction() {
      let num = 0;
      this.ids.forEach(id => {
        let params = {
          id: id,
          deletetime: null
        }
        updateOrder(params).then(response => {
        }).catch(e => {
          num++;
        });
      })
      if(num == 0) {
        this.$modal.msgSuccess("还原成功");
        this.getDelList();
      }
    },
    /** 多选销毁数据 */
    handleDestruction() {
      const ids = this.ids;
      this.$modal.confirm('是否确认删除短剧编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getDelList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 还原全部数据 */
    batchReduction() {
      if(this.delList.length > 0) {
        let num = 0;
        this.delList.forEach(item => {
          item.deletetime = null
          updateOrder(item).then(response => {
          }).catch(e => {
            num++;
          });
        })
        if(num == 0) {
          this.$modal.msgSuccess("还原成功");
          this.getDelList();
        }
      } else {
        this.$modal.msgWarning("回收站暂无数据");
      }
    },
    /** 销毁全部数据 */
    batchDestruction() {
      if(this.delList.length > 0) {
        const ids = this.delList.map(obj => obj.id);
        this.$modal.confirm('是否确认删除回收站中的全部数据？').then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getDelList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      } else {
        this.$modal.msgWarning("回收站已经是空的了");
      }
    },
    /** 行间单击还原数据 */
    clickReduction(row) {
      row.deletetime = null
      updateOrder(row).then(response => {
        this.$modal.msgSuccess("还原成功");
        this.getDelList()
      });
    },
    /** 行间单击销毁数据 */
    clickDestruction(row) {
      const ids = [];
      ids.push(row.id)
      this.$modal.confirm('是否确认删除短剧编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getDelList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    statusText(status) {
      switch (status) {
        case -2:
          return '交易关闭';
        case -1:
          return '已取消';
        case 0:
          return '未支付';
        case 1:
          return '已支付';
        case 2:
          return '已完成';
        default:
          return '';
      }
    },
    payTypeText(payType) {
      switch (payType) {
        case 'wechat':
          return '微信支付';
        case 'alipay':
          return '支付宝';
        case 'wallet':
          return '钱包支付';
        case 'score':
          return '积分支付';
        case 'cryptocard':
          return '卡密兑换';
        case 'system':
          return '管理员设置';
        case 'paypal':
          return 'PayPal';
        case 'stripe':
          return 'Stripe';
        default:
          return '';
      }
    },
    platformText(platform) {
      switch (platform) {
        case 'H5':
          return 'H5';
        case 'wxOfficialAccount':
          return '微信公众号';
        case 'wxMiniProgram':
          return '微信小程序';
        case 'Web':
          return 'Web';
        case 'App':
          return 'APP';
        default:
          return '';
      }
    },
    /** 查询分销订单列表 */
    getList() {
      this.loading = true;
      if(this.dateList != null && this.dateList.length > 0) {
        this.queryParams.beginPaytime = this.dateList[0].getTime()
        this.queryParams.endPaytime = this.dateList[1].getTime()
      } else {
        this.queryParams.beginPaytime = ''
        this.queryParams.endPaytime = ''
      }
      this.getTotalMap();
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        resellerId: null,
        orderSn: null,
        userId: null,
        langId: '',
        times: null,
        status: null,
        totalFee: null,
        payFee: null,
        currency: null,
        transactionId: null,
        paymentJson: null,
        remark: null,
        payType: null,
        paytime: null,
        ext: null,
        platform: null,
        createtime: null,
        updatetime: null,
        deletetime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分销订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分销订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let params = {
        id: row.id,
        deletetime: new Date().getTime()
      }
      updateOrder(params).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
